/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.virtual.view;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.jcr.RepositoryException;
import offset.nodes.UrlQueryConstants;
import offset.nodes.client.model.HttpUtils;
import offset.nodes.client.veditor.VirtualEditor;
import offset.nodes.server.view.ContainerBean;
import offset.nodes.server.model.RepositoryUtils;
import offset.nodes.server.view.FacesUtils;
import offset.nodes.server.html.model.PageModel;
import offset.nodes.server.view.component.applet.AppletModel;
import offset.nodes.server.virtual.controller.VirtualAttribute;

/**
 *
 * @author Walter Lütgenau
 */
public class VirtualBean extends ContainerBean {
    String path;
    String content;
    String nodeId;
    String cssPath;
    String instancePath;
    String virtualReference;
    String parentPath;
    
    protected void init() throws RepositoryException {
        VirtualAttribute att = (VirtualAttribute) FacesUtils.getRequest().getAttribute(VirtualAttribute.ATT_VIRTUAL);
        if ( att == null) {
            path = FacesUtils.getPath();
            return;
        }
        
        if (att.getPath() != null)
            this.path = att.getPath();
        this.content = att.getContent();
        this.nodeId = att.getNodeId();
        this.cssPath = att.getCssPath();
        this.instancePath = att.getInstancePath();
        this.virtualReference = att.getVirtualReference();
        this.parentPath = att.getParentPath();
    }

    protected void initComponents() {
        AppletModel exportBookModel = new AppletModel();
        exportBookModel.setRendered(false);
        getComponents().put("exportBook", exportBookModel);
    }

    public void renderExportBook(ActionEvent e) throws RepositoryException, IOException {
        AppletModel appletModel = (AppletModel) getComponents().get("exportBook");
        appletModel.setRendered(true);
        FacesUtils.refresh();
    }

    public boolean isRenderCssPath() throws RepositoryException {
        init();
        return cssPath != null;
    }

    public String getVirtualReference() {
        return virtualReference;
    }

    public void setVirtualReference(String virtualReference) {
        this.virtualReference = virtualReference;
    }

    public String edit() throws RepositoryException, IOException {
        FacesUtils.refresh();
        return "edit";
    }
    
    public String print() throws RepositoryException, IOException {
        FacesUtils.refresh();        
        return "print";
    }    

    public void remove(ActionEvent e) throws RepositoryException, IOException {
        init();
        PageModel model = new PageModel(RepositoryUtils.getRepositorySession());
        model.removeNode(nodeId);
        
        FacesContext.getCurrentInstance().getExternalContext().redirect(FacesUtils.getContextPath()+path);
    }

    public String getName() throws RepositoryException {
        init();
        int nameStart = path.lastIndexOf("/");

        if (nameStart < 0) {
            return path;
        }

        return path.substring(nameStart + 1);
    }

    public String getPath() throws RepositoryException {
        init();
        return path;
    }
    
    public String getEditUrl() throws RepositoryException {
        init();
        String editUrl;
         if(virtualReference != null && virtualReference.length() > 0)
            editUrl = virtualReference;
        else
            editUrl = path;
        
        editUrl = HttpUtils.addParameter(editUrl, UrlQueryConstants.PARAM_MODE, UrlQueryConstants.PARAMVALUE_MODE_APPLET);
        return editUrl;
    }
    
    public InputStream getContent() throws RepositoryException {
        init();
        if (content == null)
            System.out.println("Path = " + path);
        return new ByteArrayInputStream(content.getBytes());
    }

    public int getMode() {
        return VirtualEditor.MODE_VIRTUAL_PAGE_DATA_EDITOR;
    }

    public String getCssPath() throws RepositoryException {
        init();
        return cssPath;
    }

    public String getInstancePath() throws RepositoryException {
        init();
        return instancePath;
    }

    public void setCssPath(String cssPath) {
        this.cssPath = cssPath;
    }

    public void setInstancePath(String instancePath) {
        this.instancePath = instancePath;
    }

    public String getParentPath() {
        if (parentPath == null)
            return "";
        return parentPath;
    }
}
